﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Database.ConnectDatabase
{
    public class BaggagesDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblBaggage> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblBaggages select a;
                return list.ToList<tblBaggage>();
            }
            catch
            {
                return new List<tblBaggage>();
            }
        }

        public tblBaggage GetByKey(int id)
        {
            try
            {
                var obj = dbContext.tblBaggages.SingleOrDefault(a => a.Id == id);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public bool Insert(tblBaggage obj)
        {
            bool isOk = false;
            try
            {
                dbContext.tblBaggages.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                isOk = true;
            }
            catch
            {
                return false;
            }
            return isOk;
        }

        public bool Update(tblBaggage obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblBaggages.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }

        public bool Delete(int id)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblBaggages.SingleOrDefault(a => a.Id == id);
                if (exist != null)
                {
                    dbContext.tblBaggages.DeleteOnSubmit(exist);
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion
    }
}
